<template>
  <div class="table-box">
    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="addItem()">新增 </el-button>
          </el-col>
          <right-toolbar :search="false" @queryTable="getData"></right-toolbar>
        </el-row>
      </template>

      <el-table :data="tableData" style="width: 100%; height: calc(100% - 40px)" v-loading="tableLoading">
        <el-table-column prop="labels" label="词条标签" align="center" />
        <el-table-column label="操作" align="center" width="180">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="updateItem(scope.row.id)" />
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="deleteItem(scope.row.id)" />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getData" />
    </el-card>
    <el-dialog v-model="dialogFormVisible" :title="title" width="500">
      <el-form :model="form" label-width="100px" v-loading="loading">
        <el-form-item label="词条标签名称" prop="labels">
          <el-input v-model="form.labels" placeholder="请输入词条名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmBtn">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import {defineComponent, ref, onMounted} from "vue";
import {Action} from "element-plus";
import {
  deleteReportLabels,
  reportLabelsDetail,
  reportLabelsList,
  saveReportLabels,
  updateReportLabels
} from "@/api/reportLabels";

const {proxy} = getCurrentInstance() as ComponentInternalInstance;
const tableLoading = ref(false);
const tableData = ref<Record<string, any>[]>([]);
const loading = ref(false);
const title = ref('新增词条');
const dialogFormVisible = ref(false);
const initForm = () => {
  return { id: '', labels: ''}
}
const form = ref<Record<string, any>>(initForm());
const total = ref(0);
const queryParams = ref({pageNum: 1, pageSize: 10});

function getData() {
  tableLoading.value = true;
  reportLabelsList(queryParams.value).then((res) => {
    if (+res.code === 200) {
      tableData.value = res.rows || [];
      total.value = res.total || 0;
    }
  }).finally(() => {tableLoading.value = false})
}

function addItem() {
  dialogFormVisible.value = true;
  title.value = '新增行业报告标签';
  form.value = initForm();
}

function updateItem(id: string) {
  dialogFormVisible.value = true;
  title.value = '修改行业报告标签';
  getItem(id);
}

function getItem(id: string) {
  loading.value = true;
  form.value = initForm();
  reportLabelsDetail(id).then((res) => {
    form.value = res.data
  }).finally(() => loading.value = false);
}

function deleteItem(id: string) {
  ElMessageBox.alert('确认删除该数据？', '提示', {
    confirmButtonText: '确定',
    callback: (action: Action) => {
      if (action === 'confirm') {
        deleteReportLabels(id).then((res: any) => {
          if (+res.code === 200) {
            proxy?.$modal.msgSuccess("删除成功");
            getData();
          }
        })
      }
    },
  })
}

function confirmBtn()  {
  if (form.value.id) {
    updateReportLabels(form.value).then((res) => {
      if (+res.code === 200) {
        proxy?.$modal.msgSuccess("修改成功");
        getData();
        dialogFormVisible.value =  false;
      }
    })
  } else {
    saveReportLabels(form.value).then((res) => {
      if (+res.code === 200) {
        proxy?.$modal.msgSuccess("新增成功");
        getData();
        dialogFormVisible.value = false;
      }
    })
  }
}

onMounted(() => {
  getData();
})

defineComponent({name: 'ReportLabels'})
</script>

<style scoped lang="scss">
.table-box {
  width: 100%;
  height: 100%;
}
</style>
